package org.emmannuel.virtues.client.panel;


import org.emmannuel.virtues.client.common.TOVirtue;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class AdoptingPanel {
	
	final DialogBox dialog;

	VerticalPanel vp = new VerticalPanel();
	
	HTML invitationHtml = new HTML("Adopting this You agree on..");
	
	Label suggLabel = new Label("Your try:");
	TextArea suggetionText = new TextArea();
	CheckBox checkPublish = new CheckBox("Do not publish");
	
	Label remindLabel = new Label("Time of adoption:");
	ListBox reminderTime = new ListBox();
	
	Label emailLabel = new Label("E-mail:");
	TextBox eMail = new TextBox();
	
	Button adoptButton = new Button("Adopt");
	
	AdoptingPanel(){
		
		dialog = new DialogBox();
		dialog.setText("Adopting a Virtue");
		dialog.setAnimationEnabled(true);
		dialog.setAutoHideEnabled(true);
		//dialog.setSize("420px", "200px");
		
		vp.addStyleName("dialogVPanel");
		//vp.setSize("400px", "200px");
		vp.add(invitationHtml);

		reminderTime.addItem("Two weeks");
		reminderTime.addItem("Month");
		reminderTime.addItem("One and half month");
		reminderTime.addItem("Two months");
		reminderTime.setWidth("150px");
		
		Grid grid = new Grid(3,2);
		
		suggetionText.setSize("300px", "100px");
		grid.setWidget(0,0,suggLabel);
		grid.setWidget(0,1,suggetionText);
	
		HorizontalPanel hpp = new HorizontalPanel();
		hpp.add(reminderTime);
		hpp.add(checkPublish);
		hpp.setCellWidth(checkPublish, "150px");
		hpp.setCellHorizontalAlignment(checkPublish, HasHorizontalAlignment.ALIGN_RIGHT);
		hpp.setCellVerticalAlignment(checkPublish, HasVerticalAlignment.ALIGN_TOP);
		
		grid.setWidget(1,0,remindLabel);
		grid.setWidget(1,1,hpp);
	
		if(!AccountPanel.isOnline()){
		eMail.setWidth("150px");
		eMail.addFocusHandler(new FocusHandler() {
			
			@Override
			public void onFocus(FocusEvent event) {
					
			}
		});
		grid.setWidget(2,0,emailLabel);
		grid.setWidget(2,1,eMail);
		} else {
			grid.setWidget(2,0,new Label("Name:"));
			grid.setWidget(2,1,new Label(AccountPanel.getUserName()));
		}
		vp.add(grid);	

		Button closeBtn = new Button("Close");
		closeBtn.addStyleName("closeButton");
		
		closeBtn.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				dialog.hide();
			}
		});
		
		HorizontalPanel hp = new HorizontalPanel();
		adoptButton.setWidth("100px");
		adoptButton.addStyleName("sendButton");
		GWT.log("css" + adoptButton.getStyleName(), null);
		hp.add(adoptButton);
		hp.add(closeBtn);
		hp.setCellVerticalAlignment(closeBtn, HasVerticalAlignment.ALIGN_BOTTOM);
		
		vp.add(hp);
		vp.setCellHorizontalAlignment(hp, HasHorizontalAlignment.ALIGN_RIGHT);
		dialog.setWidget(vp);
		
	}
	
	public void adoptVirtue(TOVirtue virtue){
		dialog.setText(dialog.getText()+ " : " +virtue.getName());
		//invitationHtml.setText("Adopting virtue: "  + virtue.getName());
	}
	
	
	
//	
//	
//	dialogBox.setText("About virtue...");
//	dialogBox.setAnimationEnabled(true);
//	final Button closeButton = new Button("Close");
//	// We can set the id of a widget by accessing its Element
//	closeButton.getElement().setId("closeButton");
//	VerticalPanel dialogVPanel = new VerticalPanel();
//	TabPanel tp = new TabPanel();
//	TabBar tb = new TabBar();
//	dialogVPanel.addStyleName("dialogVPanel");
//	dialogVPanel.add(new TextBox());
//	// dialogVPanel.add(textToServerLabel);
//	dialogVPanel.add(new TextBox());
//	// dialogVPanel.add(serverResponseLabel);
//
//	Button adoptBtn = new Button("Adopt");
//	adoptBtn.addClickHandler(new ClickHandler() {
//
//		@Override
//		public void onClick(ClickEvent event) {
//			Window.alert("You adopted an virtue");
//		}
//	});
//	dialogVPanel.add(adoptBtn);
//	dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
//	// dialogVPanel.add(closeButton);
//	tb.addTab(dialogVPanel);
//	tp.add(tb, "Adopting");
//	tp.selectTab(0);
//
//	TabBar tbStories = new TabBar();
//	tbStories
//			.addTab(new HTML(
//					"<div style='width : 200px'><br/>Stories will come soon...<br/>...</div>"));
//	tp.add(tbStories, "Stories");
//
//	TabBar tbStat = new TabBar();
//	tbStat
//			.addTab(new HTML(
//					"<div style='width : 200px'><br/>134 people adopted this virtue.</div>"));
//	tp.add(tbStat, "Statistics");
//
//	TabBar tbSugg = new TabBar();
//	tbSugg
//			.addTab(new HTML(
//					"<div style='width : 200px'><lo><li>Listen Your father.</li><li>Keep smiling.</li></lo></div>"));
//	tp.add(tbSugg, "Suggestions");
//
//	tp.add(closeButton, "Close");
//	dialogBox.setWidget(tp);
//
//	// Add a handler to close the DialogBox
//	closeButton.addClickHandler(new ClickHandler() {
//		public void onClick(ClickEvent event) {
//			dialogBox.hide();
//		}
//	});
//
//	// Create a handler for the sendButton and nameField
//	class MyHandler implements ClickHandler {
//		/**
//		 * Fired when the user clicks on the sendButton.
//		 */
//		public void onClick(ClickEvent event) {
//			sendNameToServer(event);
//		}
//
//		/**
//		 * Send the name from the nameField to the server and wait for a
//		 * response.
//		 */
//		private void sendNameToServer(ClickEvent event) {
//
//			dialogBox.center();
//			dialogBox.show();
//		}
//	}
//
//	// Add a handler to send the name to the server
//	MyHandler handler = new MyHandler();

}
